import os
import sys
import numpy as np

loss = 1e-4 # 容忍偏差，一般fp16要求绝对误差和相对误差均不超过千分之一
minimum = 10e-10
np.set_printoptions(threshold=sys.maxsize)

def verify_result(real_result, golden):
    dtype = np.float32
    with open("./src/main.cpp", "r") as f:
        lines = f.readlines()
        for line in lines:
            if "opDesc.dtype = " in line:
                dtype = int(line.split("=")[1].strip().replace(";", ""))
                if dtype == 0:
                    dtype = np.float32
                elif dtype == 1:
                    dtype = np.float16
                elif dtype == 2:
                    dtype = np.int32
                elif dtype == 3:
                    dtype = np.double

    real_result = np.fromfile(real_result, dtype=dtype) # 从bin文件读取实际运算结果
    golden = np.fromfile(golden, dtype=dtype) # 从bin文件读取预期运算结果
    result = np.abs(real_result - golden) # 计算运算结果和预期结果偏差
    deno = np.maximum(np.abs(real_result), np.abs(golden))  # 获取最大值并组成新数组
    result_atol = np.less_equal(result, loss) # 计算绝对误差
    result_rtol = np.less_equal(result / np.add(deno, minimum), loss) # 计算相对误差
    if not result_rtol.all() and not result_atol.all():
        if np.sum(result_rtol == False) > real_result.size * loss and np.sum(result_atol == False) > real_result.size * loss: # 误差超出预期时返回打印错误，返回对比失败
            print("[ERROR] result error")
            print(f"Expected {golden}, Shape {golden.shape}, Got {real_result}, Shape {real_result.shape}")
            return False
    print("test pass")
    return True

if __name__ == '__main__':
    verify_result(sys.argv[1],sys.argv[2])
